/**
 * Copyright (c) 2024,  科泰集团教研团队
 * 版权所有，请勿侵权
 */
package com.ktjiaoyu.ktmall.service.impl;

import com.ktjiaoyu.ktmall.dao.AdminUserDao;
import com.ktjiaoyu.ktmall.dao.impl.AdminUserDaoImpl;
import com.ktjiaoyu.ktmall.entity.AdminUser;
import com.ktjiaoyu.ktmall.service.AdminUserService;
import com.ktjiaoyu.ktmall.utils.Page;

import java.sql.SQLException;
import java.util.List;

/**
 * 管理员信息表，用于登录后台 Service接口实现
 * @author 科泰集团教研团队
 * @createTime 2024-4-9 17:36:33
 * @lastModify 2024-4-9 17:36:33
 * @version 1.0.0
 */
public class AdminUserServiceImpl implements AdminUserService {

    private AdminUserDao adminUserDao = new AdminUserDaoImpl();


    @Override
    public AdminUser login(String username, String password) {
        AdminUser admin = adminUserDao.getByUsername(username);
        if(admin != null && admin.getPassword().equals(password)){
            return admin;
        }
        return null;
    }

    @Override
    public Page<AdminUser> findPage(AdminUser entity, int pageNo, int pageSize) {
        Page<AdminUser> page = new Page<>();
        // 获得总数量
        int count = adminUserDao.count(entity);
        page.setPageNo(pageNo);
        page.setPageSize(pageSize);
        // 设置总数量，计算总页数
        page.setTotalCount(count);
        List<AdminUser> list = adminUserDao.getPage(entity, pageNo, pageSize);
        page.setData(list);
        return page;
    }

    @Override
    public List<AdminUser> findAll() {
        return adminUserDao.getAll();
    }

    @Override
    public AdminUser findById(Long id) {
        return adminUserDao.getById(id);
    }

    @Override
    public boolean saveOrUpdate(AdminUser entity) throws SQLException {
        if(entity.getId() != null){
            // 如果有ID就是修改
            return adminUserDao.update(entity) > 0;
        }
        // 没有id就是新增
        return adminUserDao.insert(entity) > 0;
    }

    @Override
    public boolean importBatch(AdminUser... entities) {
        return adminUserDao.insertBatch(entities) != null;
    }


    @Override
    public boolean removeById(Long id) {
        return adminUserDao.deleteById(id) > 0;
    }

    @Override
    public boolean removeByEntity(AdminUser entity) {
        return adminUserDao.deleteByEntity(entity) > 0;
    }

    @Override
    public boolean removeBatch(Long... ids) {
        return adminUserDao.deleteBatch(ids) != null;
    }
}
